package com.globallogic.javatrainings.sergiiivanov.model;

import java.io.Serializable;

/**
 * Stores information about teacher<br>
 * Contains links collections to assigned {@link Course courses} and given
 * {@link ScoreRecord score records}
 * 
 * @author Sergey
 * 
 */
public class Teacher extends Person implements Serializable {
    private static final long serialVersionUID = 1L;
    private MyArray<Course> courses;
    private MyArray<ScoreRecord> scores;

    /**
     * Quick constructor
     * 
     * @param name
     *            - String; not null, not empty
     * @param mobile
     *            - free string
     * @param email
     *            - free string
     * @throws UniversityWarning
     *             if name is null or empty
     */
    public Teacher(String name, String mobile, String email) throws UniversityWarning {
        super(name, mobile, email);
        courses = new MyArray<Course>();
        scores = new MyArray<ScoreRecord>();
    }

    /**
     * Default constructor. Used by TeachersManager.addPerson()
     */
    public Teacher() {
        super();
        courses = new MyArray<Course>();
        scores = new MyArray<ScoreRecord>();
    }

    /**
     * Removes links to the teacher. Used before removing teacher.<br>
     * removes links from: courses, scores
     */
    void removeLinks() {
        for (Course c : courses.copy()) {
            if (this.equals(c.getTeacher())) {
                c.setTeacher(null);
                removeCourse(c);
            }
        }
        courses.clear();
        for (ScoreRecord s : scores.copy()) {
            if (this.equals(s.getTeacher()))
                s.removeTeacher();
        }
        scores.clear();
        super.resumeClear();
    }

    /**
     * Links teacher to the {@link Course}
     * 
     * @param c
     *            {@link Course} to link
     */
    public void addCourse(Course c) {
        if (c != null) {
            if (courses.indexOf(c) < 0) {
                courses.add(c);
            }
            if (!this.equals(c.getTeacher())) {
                c.setTeacher(this);
            }
        }
    }

    /**
     * Removes link to the {@link Course}
     * 
     * @param c
     *            Course to unlink
     */
    public void removeCourse(Course c) {
        if(c != null){
            if( this.equals(c.getTeacher()) ) c.clearTeacher();
        }
        courses.remove(c);
    }

    /**
     * @return copy of linked courses collection as {@link MyArray} of
     *         {@link Course}
     */
    public MyArray<Course> getCourses() {
        return courses.copy();
    }

    /**
     * Calls {@link ScoresProcessor} to create scores table
     * 
     * @return scores table as String[][]
     */
    public String[][] getScoreTable() {
        ScoresProcessor sp = new ScoresProcessor();
        sp.setScoresCollection(scores);
        return sp.getTable();
    }

    /**
     * Links {@link ScoreRecord} to the teacher
     * 
     * @param scoreRecord
     *            {@link ScoreRecord} to link
     */
    void addScoreLink(ScoreRecord scoreRecord) {
        if (scores.indexOf(scoreRecord) < 0) {
            scores.add(scoreRecord);
        }
    }

    /**
     * @param scoreRecord
     */
    void removeScoreLink(ScoreRecord scoreRecord) {
        if (scores.indexOf(scoreRecord) > -1) {
            scores.remove(scoreRecord);
        }

    }

}
